/* To compile me in Linux, type:    gcc -o client client.c  */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <errno.h>
#include <unistd.h>

main(int argc, char *argv[])
{
   struct  hostent  *h;   
   struct  sockaddr_in server_address;  
   int     sd;               
   int     port;             
   char    *host;            
   int     n;                
   char    buf[1000];        

     memset((char *)&server_address ,0,sizeof(server_address)); 
     server_address.sin_family = AF_INET;

   if (argc != 4){ 
     cerr << "Wrong number of arguments - usage: udp_client hostname port message" << endl;
     exit(1);
	 }

   port = atoi(argv[2]);
   server_address.sin_port = htons((u_short)port);
   host = argv[1];

   h = gethostbyname(host);
   if( ((char *)h) == NULL)
   { 
    cerr << "invalid host:  " <<  host << endl;
    exit(1);
   }
   
   memcpy(&server_address.sin_addr, h->h_addr, h->h_length);

   sd = socket(PF_INET, SOCK_DGRAM, 0);
   if (sd < 0){ 
       cerr << "Socket creation failed" << endl;
       exit(1);
     }
   cout << argv[2] << " . " << argv[3] << endl;
   n = sendto(sd, argv[3], strlen(argv[3]), 0, 
              (struct sockaddr *)&server_address, sizeof(struct sockaddr) );
   if (n == -1)
     cerr << "Error sending data" << endl;
	 else	
     cout << "Sent " << argv[3] <<": "<< n << " bytes." << endl;
   
   close(sd);

   exit(0);
}



